/**
*  \file nvram.c
*
*  \brief
*
*  \author Michael J. Hohmann  <michael.hohmann@linde-mh.de>
*
*
*/

/*===============================================[ private includes  ]================================================*/
#include "nvram.h"
#include "../assertor/assertor.h"
#include "../sdfile/sdfile.h"
#include "../mcu/mcu.h"
#include "../endian/endian_read.h"
#include "../systime/systime.h"
#include "../i2cgi_sim/i2cgi_sim.h"

/*===============================================[ private defines   ]================================================*/
#define NVRAM_SD (&nvram_sd.sd)
/*===============================================[ private datatypes ]================================================*/

/** Private variables */
struct sdfile_obj nvram_my_sdfile;
struct sd24xx256_obj nvram_sd;

U32 last_address;
/*===============================================[ private variables ]================================================*/

/*===============================================[ private functions ]================================================*/
static void set_wp(BO value );
static BO nvram_i2c_write( enum i2cgi_cmd cmd, U8 ctrl, void* data, pal_size_t len );

/*===============================================[ inline functions  ]================================================*/

/*===============================================[ public functions  ]================================================*/
RC nvram_initialize( void )
{
   sd24xx256_construct_sim(&nvram_sd, nvram_i2c_write, set_wp, systime_get, 10 );/* used in simulation*/
   return sdfile_construct( &nvram_my_sdfile, "nvram.bin", 0x8000, 0xFF, FALSE );
}

/*
struct sd* nvram_sd( void )
{
	return &nvram_sd.sd;
}*/

static void set_wp(BO value )
{
   mcu_set_output(mcu_output_I2C_WP_CPU_M,value);
}

static U8 i2c_master_read(U32 address, U8* data, U32 size)
{

}

static U8 i2c_master_write(U32 address, U8* data, U32 size)
{

}


static BO nvram_i2c_write( enum i2cgi_cmd cmd, U8 ctrl, void* data, pal_size_t len )
{
   if(( cmd & i2cgi_cmd_start)&&!(ctrl & i2cgi_ctrl_read))
   {
      last_address= (U32) endian_read_u16_big(data);
      last_address|=( (ctrl&0x0E)<<13);
      len-=2;
   }

   if(len)
   {
      if( cmd & i2cgi_cmd_stop)
      {
         if( ctrl & i2cgi_ctrl_read)
         {
            sd_read(&nvram_my_sdfile.dev,data,(void*)last_address,len);
         }
         else
         {
            sd_write( &nvram_my_sdfile.dev,(void*)last_address,data ,len);
         }
       }
   }
   return TRUE;
}



/*===============================================[ private functions ]================================================*/


/***********************************************************************************************************************
*                             Copyright 2018 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
